﻿public ActionResult GridData(int? id = 0)
        {
            int total;
            GridRequest request = new GridRequest(Request);
            if (id.HasValue && id.Value > 0)
            {
                request.FilterGroup.Rules.Add(new FilterRule("hr_department.Id", id.Value));
            }
            var datas = GetQueryData<hr_post, int>(Ihr_postContract.IQueryableOfEntity, out total, request)
                .Select(m => new
            {
                m.Id,
                m.post_name,
                departmentname = m.hr_department.d_name,
                positionname = m.hr_position.position_name,
                postnote = m.note,
                m.CreatedTime,
                m.post_descript,
            });
            return Json(new GridData<object>(datas, total), JsonRequestBehavior.AllowGet);
        }
        [AjaxOnly]
        public ActionResult NodeData()
        {
            Func<hr_department, object> getNodeData = null;
            getNodeData = entity =>
            {
                dynamic node = new ExpandoObject();
                node.id = entity.Id;
                node.text = entity.d_name;
                node.url = "";
                node.children = new List<dynamic>();
                var children = entity.Children.OrderBy(m => m.d_order).ToList();
                foreach (var child in children)
                {
                    node.children.Add(getNodeData(child));
                }
                return node;
            };
            List<hr_department> roots = Ihr_departmentContract.IQueryableOfEntity.Where(m => m.Parent == null).OrderBy(m => m.d_order).ToList();
            List<object> nodes = roots.Select(getNodeData).ToList();
            return Content(JsonHelper.ToJson(nodes), "application/json");
        }
        
        
        
        

@{
    ViewBag.Title = "岗位管理";
    Layout = "~/Views/Shared/_DataGridLayoutWithdialog.cshtml";
    ViewBag.GridId = "post";
    ViewBag.TreeId = "department";
    ViewBag.GridDataUrl = Url.Action("GridData");
    ViewBag.NodeDataUrl = Url.Action("NodeData");
    ViewBag.AddUrl = Url.Action("Add");
    ViewBag.EditUrl = Url.Action("Edit");
    ViewBag.DeleteUrl = Url.Action("Delete");
    ViewBag.Form = Url.Action("form");
    ViewBag.Detail = Url.Action("Detail");
}
@section customScript
{
    <script type="text/javascript">
        var selectTreeData = null;
        var newdialog = function () {
            $.easyui.showDialog({
                topMost: false, title: '@ViewBag.Title-新增', top: 10, width: 400, height: 300,
                href: '@ViewBag.Form',
                enableSaveButton: false, enableApplyButton: false,
                buttons: [
                       {
                           text: "保存", iconCls: "icon-save", handler: function (dia) {
                               var validate = $("#form_hr_post").form('validate');
                               if (!validate) {
                                   return false;
                               }
                               var ff = $('#form_hr_post').serializeJsonArry();
                               //console.info(JSON.stringify(ff));
                               $.post("@ViewBag.AddUrl", { dtos: JSON.stringify(ff) }, ResultHandler(dia));
                           }
                       },
                ],
            });
        }
        var Editdialog = function () {
            var row = grid.datagrid("getSelected");
            if (!row) {
                $.osharp.easyui.msg.tip("请选择要编辑的行。");
                return;
            }
            $.easyui.showDialog({
                topMost: false, title: '@ViewBag.Title-编辑', top: 10, width: 400, height: 220,
                enableSaveButton: false, enableApplyButton: false,
                href: '@ViewBag.Form',
                onLoad: function () {
                    $.get('@ViewBag.Detail', { Id: row.Id }, function (data) {
                        $('#form_hr_post').form('load', data);
                        $('#hr_departmentnamecombo').combotree('setValue', data.hr_departmentname);
                        $('#hr_positionnamecombo').combogrid('setValue', data.hr_positionname);
                    })
                },
                buttons: [
                       {
                           text: "保存", iconCls: "icon-save", handler: function (dia) {
                               var validate = $("#form_hr_post").form('validate');
                               if (!validate) {
                                   return false;
                               }
                               var ff = $('#form_hr_post').serializeJsonArry();
                               //console.info(JSON.stringify(ff));
                               $.post("@ViewBag.EditUrl", { dtos: JSON.stringify(ff) }, ResultHandler(dia));
                           }
                       },
                ],
            });
        };
        columns = [[
            { field: "Id", title: "编号", width: 40, halign: "center", align: "right", sortable: true },
            { field: "post_name", title: "岗位名称", width: 80, sortable: true },
            { field: "departmentname", title: "组织机构", width: 80, sortable: true, },
            { field: "positionname", title: "职务级别", width: 80, sortable: true, },
            { field: "postnote", title: "岗位说明", width: 80, sortable: true },
            { field: "CreatedTime", title: "创建时间", width: 150, halign: "center", align: "center", sortable: true, formatter: function (value) { return $.osharp.tools.formatDate(value); } }
        ]];
        var add = { text: "增加", iconCls: "icon-add", handler: newdialog };
        toolbarData.push(add);
        var edit = { text: "编辑", iconCls: "icon-edit", handler: Editdialog };
        toolbarData.push(edit);
        startfunction = function () {
            //导出
        };

        endfunction = function () {
            var tree = $("#tree-@ViewBag.TreeId");
            tree.tree({
                url: "@ViewBag.NodeDataUrl",
                onClick: function (node) {
                    if (node == null) return;
                    selectTreeData = node;
                    grid.datagrid({
                        url: "@ViewBag.GridDataUrl/" + node.id,
                        onBeforeEdit: function (index, row) {
                            row.department.Id = node.id
                        }
                    });
                }
            });
            grid.datagrid({
                onBeforeEdit: function (index, row) {
                    if (selectTreeData == null) {
                        $.osharp.easyui.msg.error("请选择组织机构，再进入编辑。");
                        return false;
                    }
                }
            });
        };


    </script>
}
@section headHtml
{
    <div id="layout-@ViewBag.GridId" class="easyui-layout" data-options="fit:true">
        <div data-options="region:'west', split:true, minWidth:80, width:150, title:'组织机构'">
            <ul id="tree-@ViewBag.TreeId" class="easyui-tree"></ul>
        </div>
        <div data-options="region:'center', border:false">
}
@section footHtml
{
        </div>
    </div>
}